package com.hgxd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.entity.res.ResOptions;
import com.hgxd.entity.vo.PowerOffVo;
import com.hgxd.exception.ServiceException;
import com.hgxd.result.Result;
import com.hgxd.result.ResultCodeEnum;
import com.hgxd.service.SysRoleService;
import com.hgxd.service.SysUserService;
import com.hgxd.utils.security.JwtHelper;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hgxd.service.IPowerOffService;
import com.hgxd.entity.pojo.PowerOff;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 停送电管理 前端控制器
 * </p>
 *
 * @author wtf
 * @since 2023-11-30
 */
@Api(tags = "停送电管理")
@RestController
@RequestMapping("/api/powerOff")
public class PowerOffController {
    @Resource(name = "PowerOffServiceImpl")
    private IPowerOffService powerOffService;

    @Resource(name = "sysRoleServiceImpl")
    private SysRoleService sysRoleService;

    @Resource(name = "SysUserServiceImpl")
    private SysUserService sysUserService;

    /**
     * 获取对应一二级审批人信息，并返回为options格式（前端用）
     *
     * @return
     */
    @GetMapping("getApprover")
    @ApiOperation(value = "一二级审批人信息options")
    public Result<Map<String, List<ResOptions>>> getApprover() {
        return Result.ok(sysUserService.getApprover());
    }

    /**
     * 获取对应操作人信息，并返回为options格式（前端用）
     *
     * @return
     */
    @GetMapping("getOperate")
    @ApiOperation(value = "操作人信息options")
    public Result<List<ResOptions>> getOperate() {
        return Result.ok(powerOffService.getOperate());
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "无需传入id")
    public Result<Boolean> insert(@RequestBody PowerOff powerOff) {
        return Result.ok(powerOffService.save(powerOff));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "必须传入id")
    public Result<Boolean> update(@RequestBody PowerOff powerOff) {
        return Result.ok(powerOffService.updateById(powerOff));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "必须传入id")
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(powerOffService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入ids")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(powerOffService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    public Result<List<PowerOff>> findAll() {
        return Result.ok(powerOffService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个", notes = "传入id")
    public Result<PowerOff> findOne(@PathVariable Integer id) {
        return Result.ok(powerOffService.getById(id));
    }

    /**
     * 分页模糊查询，分权限 1：一级审批人，2：二级审批人，3：申请人
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "关于operateId 如果是3，则传入3对应的id 如果是1和2，则按需传入即可")
    public Result<Page<PowerOff>> findPage(@RequestBody PowerOffVo powerOffVo, HttpServletRequest request) {
        //根据token查询对应的身份
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        List<Long> roleByUserId = sysRoleService.getRoleByUserId(userId);
        for (Long l : roleByUserId) {
            if (l.intValue() == 9) {
                powerOffVo.setType(1);
                break;
            }
            if (l.intValue() == 12) {
                powerOffVo.setType(2);
                break;
            }
            if (l.intValue() == 8) {
                powerOffVo.setType(3);
                break;
            }
        }
        Integer type = powerOffVo.getType();
        if (!(type == 1 || type == 2 || type == 3)) {
            throw new ServiceException(ResultCodeEnum.ARGUMENT_VALID_ERROR, "非法！");
        }
        System.out.println("powerOffVo = " + powerOffVo);
        return Result.ok(powerOffService.findPage(powerOffVo));
    }
}

